<template>
  <div>
    <a-modal v-model:visible="modalVisible" @ok="save" :title="modalType == 0 ? '编辑手机短信' : '编辑系统通知'" @cancel="cancle">
      <a-form :labelCol="labelCol" ref="formRef" :rules="rules" :model="form">
        <a-form-item label="发送对象">共{{ longs }}个用户</a-form-item>
        <!-- 编辑系统通知显示信息 -->
        <a-form-item label="标题" v-if="modalType == 1" name="title">
          <a-input placeholder="请输入标题" v-model:value="form.title"></a-input>
        </a-form-item>
        <a-form-item label="内容" name="content">
          <a-textarea placeholder="请输入内容" v-model:value="form.content"></a-textarea>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<script>
import { message, Modal } from 'ant-design-vue'
import { ref, reactive, computed } from 'vue'
import { saveSms, saveMsg } from '@/api/setting/index'
import { useStore } from 'vuex'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    modalType: {
      type: Number,
      default: 0 //0 发送短信  1系统通知
    },
    userType: {
      type: Number,
      default: 0 //0 护士 1用户
    },
    // 电话号码
    list: {
      type: Array,
      default: []
    },
    longs: {
      type: Number,
      default: 0
    },
    // 护士带筛选条件
    nurseDto: {
      type: Object,
      default: null
    },
    // 用户带筛选条件
    userDto: {
      type: Object,
      default: null
    }
  },

  setup(props, { emit, attrs }) {
    console.log(props, 'props')
    const modalVisible = computed({
      get: () => props.visible,
      set: val => {
        emit('update:visible', val)
      }
    })
    const store = useStore()
    const form = reactive({})
    const rules = {
      content: [{ required: true, message: '请输入发送内容!', trigger: 'blur' }],
      title: [{ required: true, message: '请输入系统通知标题!', trigger: 'blur' }]
    }
    const formRef = ref('')

    const save = () => {
      console.log(props.userType, '哪个端口的', props.modalType, '什么类型')

      //   发送短信
      if (props.modalType == 0) {
        form.publishId = store.state.userId
        form.type = props.userType
        let flag = false
        // 是护士

        if (['1', '3'].indexOf(props.userType) > -1) {
          form.nurseDto = {}

          if (Object.keys(props.nurseDto).length > 0) {
            form.nurseDto = props.nurseDto
            flag = true
          }
        } else {
          // 用户
          form.userDto = {}

          if (Object.keys(props.userDto).length > 0) {
            form.userDto = props.userDto
            flag = true
          }
        }
        console.log(props.list, '0000000-----------')
        let ids = ['4', '3'].indexOf(props.userType) > -1 ? '' : props.list.join(',')
        if (ids) {
          form.ids = ids
        }
        formRef.value.validate().then(async () => {
          let res = await saveSms({ ...form })
          if (res.success) {
            message.success('发送成功')
            cancle()
          }
        })
      } else {
        form.ids = props.list
        form.publishId = store.state.userId
        form.type = props.userType
        //   系统通知
        formRef.value.validate().then(async () => {
          let res = await saveMsg(form)
          if (res.success) {
            message.success('发送成功')
            cancle()
          }
        })
      }
      //   emit("closeModal");
    }
    const cancle = () => {
      emit('closeModal')
    }
    const title = ref('编辑发送短信')
    return {
      save,
      labelCol: { style: { fontWeight: 'bold' } },
      form,
      cancle,
      title,
      rules,
      formRef,
      modalVisible
    }
  }
}
</script>
